package vip.justlive.frost.center.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * security配置
 *
 * @author wubo
 */
@Configuration
@EnableWebSecurity
public class SecurityConf implements WebMvcConfigurer {

  @Bean
  SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.formLogin(form -> form.loginPage("/login.html").loginProcessingUrl("/login").permitAll());
    http.logout(logout -> logout.logoutUrl("/logout").logoutSuccessUrl("/login.html"));
    http.csrf(AbstractHttpConfigurer::disable);
    http.headers(headers -> headers.frameOptions().disable());
    http.authorizeHttpRequests()
        .antMatchers("/**/ui/**", "/**/css/**", "/**/js/**", "/**/images/**")
        .permitAll()
        .anyRequest()
        .authenticated();
    http.httpBasic(Customizer.withDefaults());
    return http.build();
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
        .addResourceHandler("/ui/**")
        .addResourceLocations("classpath:/META-INF/spring-boot-admin-server-ui/")
        .resourceChain(true);
  }
}
